# -*- coding: utf-8 -*-
import os
import sys
import logging

# Google App Hosting imports.
from google.appengine.ext.webapp import util
from google.appengine.dist import use_library

os.environ["DJANGO_SETTINGS_MODULE"] = "gpxe.settings"
sys.path.append("/home/josh/programming/cobratechtools/")

use_library('django', '1.1')

# Enable info logging by the app (this is separate from appserver's
# logging).
logging.getLogger().setLevel(logging.DEBUG)

def log_exception(*args, **kwds):
  logging.exception('Exception in request:')

# Force sys.path to have our own directory first, so we can import from it.
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

import django.core.handlers.wsgi
import django.core.signals
import django.db

# Log errors.
django.dispatch.Signal.connect(
   django.core.signals.got_request_exception, log_exception)

# Unregister the rollback event handler.
django.dispatch.Signal.disconnect(
django.core.signals.got_request_exception,
django.db._rollback_on_exception)

def main():    
    # Create a Django application for WSGI.
    application = django.core.handlers.wsgi.WSGIHandler()

    # Run the WSGI CGI handler with that application.
    util.run_wsgi_app(application)

if __name__ == "__main__":
    main()